#!/bin/bash

# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# 执行该脚本时需进入到脚本所在目录
ROOT_DIR=$(pwd)
API_VERSION=12                                                             # 三方库对应API版本，用于记录SDK路径,必须和"compileSdkVersion"字段表示的API版本保持一致
SDK_DIR=$ROOT_DIR/../ohos-sdk-$API_VERSION/linux/$API_VERSION               # SDK路径（流水线环境中SDK路径）
LYCIUM_TOOLS_URL=https://gitcode.com/openharmony-sig/tpc_c_cplusplus.git
LYCIUM_ROOT_DIR=$ROOT_DIR/tpc_c_cplusplus
LYCIUM_TOOLS_DIR=$LYCIUM_ROOT_DIR/lycium
LYCIUM_THIRDPARTY_DIR=$LYCIUM_ROOT_DIR/thirdparty
OHPM_TOOLS=https://gitee.com/wurep/ohpm-tools.git
CMAKE_TOOLS=https://gitee.com/wurep/cmake-tools.git
CMAKE_ROOT_DIR=$ROOT_DIR/cmake-tools
OHPM_ROOT_DIR=$ROOT_DIR/ohpm-tools
DEPENDS_DIR=$ROOT_DIR/doc                                                   # 依赖库编译脚本在仓库中的位置
NGHTTP2_NAME=nghttp2                                                   # 依赖库名
OPENSSL_NAME=openssl_quic
JSON_NAME=json
LIBXML2_NAME=libxml2
NGHTTP3_NAME=nghttp3
XZ_NAME=xz
ZLIB_NAME=zlib

CI_OUTPUT_DIR=$ROOT_DIR/../out/tpc/                                         # hap/har安装目录

function prepare_lycium_tools()
{
    local commands=("gcc" "make" "pkg-config" "autoconf" "autoreconf" "automake" "patch" "libtool" "autopoint" "gperf" \
    "tcl8.6-dev" "wget" "unzip" "gccgo-go" "flex " "bison" "premake4" "python3" "python3-pip" \
    "ninja-build" "meson" "sox" "gfortran" "subversion" "build-essential" "module-assistant" " gcc-multilib" \
    "g++-multilib" "libltdl7-dev" "cabextract" "libboost-all-dev" "libxml2-utils" "gettext" "libxml-libxml-perl" \
    "libxml2" "libxml2-dev" "libxml-parser-perl" "texinfo" "libtool-bin" "xmlto" "po4a" "yasm" "nasm" "xutils-dev" \
    "libx11-dev" "xtrans-dev" "gfortran-arm-linux-gnueabi" "gfortran-aarch64-linux-gnu")

    apt update >> /dev/null

    for cmd in ${commands[@]}
    do
        which $cmd >> /dev/null
        if [ $? -ne 0 ]
        then
            echo "install $cmd"
            apt install $cmd -y >> /dev/null
        fi
    done
}


function prepare_lycium()
{
    if [ -d $LYCIUM_ROOT_DIR ]
    then
        rm -rf $LYCIUM_ROOT_DIR
    fi

    git clone $LYCIUM_TOOLS_URL -b support_x86 --depth=1
    if [ $? -ne 0 ]
    then
        return 1
    fi

    cd $LYCIUM_TOOLS_DIR/Buildtools
    tar -zxvf toolchain.tar.gz
    if [ $? -ne 0 ]
    then
        echo "unpack sdk toolchain failed!!"
        cd $OLDPWD
        return 1
    fi

    cp toolchain/* $SDK_DIR/native/llvm/bin/
    prepare_lycium_tools
    ret=$?
    cd $OLDPWD
    return $ret
}



function prepare_ohpm()
{
    if [ -d $OHPM_ROOT_DIR ]
    then
        rm -rf $OHPM_ROOT_DIR
    fi

    git clone $OHPM_TOOLS
    if [ $? -ne 0 ]
    then
        return 1
    fi

    cd $OHPM_ROOT_DIR
    tar -xvf ohpm.tar
    if [ $? -ne 0 ]
    then
        echo "unpack ohpm failed!!"
        cd $ROOT_DIR
        return 1
    fi
    cd $ROOT_DIR/library
    chmod 777 $OHPM_ROOT_DIR/ohpm/bin/ohpm
    mkdir -p /root/.ohpm
    touch /root/.ohpm/.ohpmrc
    $OHPM_ROOT_DIR/ohpm/bin/ohpm config set registry https://repo.harmonyos.com/ohpm/
    $OHPM_ROOT_DIR/ohpm/bin/ohpm install
    if [ $? -ne 0 ]
    then
        echo "ohpm install failed!!"
        cd $ROOT_DIR
        return 1
    fi
    cp $ROOT_DIR/protobufjs/index.d.ts  $ROOT_DIR/library/oh_modules/@ohos/protobufjs/src/main/ets
    sed '15s/"buffer"/"@ohos.buffer"/g' $ROOT_DIR/library/oh_modules/@ohos/protobufjs/src/main/ets/dist/protobuf.js > protobuf.js
    cp protobuf.js $ROOT_DIR/library/oh_modules/@ohos/protobufjs/src/main/ets/dist
    cd $ROOT_DIR
    $OHPM_ROOT_DIR/ohpm/bin/ohpm install @ohos/protobufjs@2.1.0
    cp $ROOT_DIR/protobufjs/index.d.ts  $ROOT_DIR/oh_modules/.ohpm/@ohos+protobufjs@2.1.0/oh_modules/@ohos/protobufjs/src/main/ets
    cp $ROOT_DIR/library/oh_modules/@ohos/protobufjs/src/main/ets/dist/protobuf.js $ROOT_DIR/oh_modules/.ohpm/@ohos+protobufjs@2.1.0/oh_modules/@ohos/protobufjs/src/main/ets/dist
    ret=$?
    cd $ROOT_DIR
    return $ret
}

function prepare_cmake()
{
    if [ -d $CMAKE_ROOT_DIR ]
    then
        rm -rf $CMAKE_ROOT_DIR
    fi

    git clone $CMAKE_TOOLS
    if [ $? -ne 0 ]
    then
        return 1
    fi

    cd $CMAKE_ROOT_DIR
    tar -xvf cmake-3.26.1-linux-x86_64.tar.gz
    if [ $? -ne 0 ]
    then
        echo "unpack cmake failed!!"
        cd $ROOT_DIR
        return 1
    fi
   export CMAKE_HOME=$CMAKE_ROOT_DIR/cmake-3.26.1-linux-x86_64
   export PATH=$CMAKE_HOME/bin:$PATH
   echo "==================查找OHOS.cmake============"
   ls $SDK_DIR/native/build-tools/cmake/share
   cp $SDK_DIR/native/build-tools/cmake/share/cmake-3.16/Modules/Platform/OHOS.cmake $CMAKE_HOME/share/cmake-3.26/Modules/Platform
   ret=$?
   cd $ROOT_DIR
   return $ret
}

function copy_depends()
{
    local dir=$1
    local name=$2

    if [ -d $LYCIUM_THIRDPARTY_DIR/$name ]
    then
        rm -rf $LYCIUM_THIRDPARTY_DIR/$name
    fi
    cp -arf $dir/$name $LYCIUM_THIRDPARTY_DIR/
}

function prepare_depends()
{
    copy_depends $DEPENDS_DIR $JSON_NAME
    copy_depends $DEPENDS_DIR $NGHTTP2_NAME
    copy_depends $DEPENDS_DIR $NGHTTP3_NAME
    copy_depends $DEPENDS_DIR $OPENSSL_NAME
    copy_depends $DEPENDS_DIR $XZ_NAME
    copy_depends $DEPENDS_DIR $ZLIB_NAME
    copy_depends $DEPENDS_DIR $LIBXML2_NAME
}

function check_sdk()
{
    if [ ! -d $SDK_DIR ]
    then
        return 1
    fi

    export OHOS_SDK=$SDK_DIR
    return 0
}

function start_build()
{
    local result=0
    cd $LYCIUM_TOOLS_DIR
    if [ $? -ne 0 ]
    then
        return 1
    fi

    bash build.sh $NGHTTP2_NAME $OPENSSL_NAME $JSON_NAME
    result=$?
    cd $OLDPWD
    return $result
}

function install_depends()
{
    local install_dir=$ROOT_DIR/library/src/main/cpp/thirdparty/
    mkdir -p $install_dir
    cp -arf $LYCIUM_TOOLS_DIR/usr/$NGHTTP2_NAME $install_dir/nghttp2
    if [ $? -ne 0 ]
    then
        echo "nghttp2 build failed!"
        return 1
    fi
    cp -arf $LYCIUM_TOOLS_DIR/usr/$OPENSSL_NAME $install_dir/openssl_quic
    if [ $? -ne 0 ]
    then
        echo "openssl_quic build failed!"
        return 1
    fi
    cp -arf $LYCIUM_TOOLS_DIR/usr/$JSON_NAME $install_dir/json
    if [ $? -ne 0 ]
    then
        echo "json build failed!"
        return 1
    fi

    if [ -d $CI_OUTPUT_DIR ]
    then
        cp -arf $LYCIUM_TOOLS_DIR/usr/$NGHTTP2_NAME $CI_OUTPUT_DIR
        cp -arf $LYCIUM_TOOLS_DIR/usr/$OPENSSL_NAME $CI_OUTPUT_DIR
        cp -arf $LYCIUM_TOOLS_DIR/usr/$JSON_NAME $CI_OUTPUT_DIR
    fi
    sed -i 's/target_link_libraries(http2_napi PUBLIC ${NATIVERENDER_ROOT_PATH}\/thirdparty\/nghttp2\/${OHOS_ARCH}\/lib\/libnghttp2.so.14)/target_link_directories(http2_napi PUBLIC ${NATIVERENDER_ROOT_PATH}\/thirdparty\/nghttp2\/${OHOS_ARCH}\/lib)\ntarget_link_libraries(http2_napi PUBLIC nghttp2)/' $ROOT_DIR/library/src/main/cpp/CMakeLists.txt
    if [ $? -ne 0 ]
    then
        echo "sed failed!"
        return 1
    fi
    return 0
}

function prebuild()
{
    prepare_cmake
    prepare_ohpm
    if [ $? -ne 0 ]
    then
        echo "ERROR: prepare_ohpm failed!!!"
        return 1
    fi
    check_sdk
    if [ $? -ne 0 ]
    then
        echo "ERROR: check_sdk failed!!!"
        return 1
    fi
    prepare_lycium
    if [ $? -ne 0 ]
    then
        echo "ERROR: prepare_lycium failed!!!"
        return 1
    fi
    prepare_depends
    start_build
    if [ $? -ne 0 ]
    then
        echo "ERROR: start_build failed!!!"
        return 1
    fi
    install_depends
    if [ $? -ne 0 ]
    then
        echo "ERROR: install depends failed!!!"
        return 1
    fi
    echo "prebuild success!!"

    return 0
}

prebuild $*
ret=$?
echo "ret = $ret"
exit $ret

#EOF
